%% Reproduce_Simulations.m
%  with this file it is possible to generate the simulations with each of
%  the parameters' set. It will generate a folder for each model.
%  The generation 1 models are the models of ALL-PRE,
%  the generation 2 models are the models of SHAM-POST,
%  the generation 3 models are the models of RIGHT-POST, and
%  the generation 4 models are the models of LEFT-POST,
%
%  Estimated run time: 3 hours 45 minutes with 4 cores @2.30GHz and SSD.
%  Minimum free space on disk: 2 GB


%% INITIALIZATION and CHECK IF ALL NECESSARY FILES ARE PRESENT

clear
close all
clc
tic

if (  exist('DataSet.mat','file')~=2 ...
   || exist('_egidio2iPurkinje.dat','file')~=2 ...
   || exist('_egidio2iGranule.dat','file')~=2 ...
   || exist('_egidio2iGolgi.dat','file')~=2 ...
   || exist('_egidio2iPurkinje.cfg','file')~=2 ...
   || exist('_egidio2iGranule.cfg','file')~=2 ...
   || exist('_egidio2iGolgi.cfg','file')~=2 ...
   || exist('NetworkEBCC.cfg','file')~=2 ...
   || exist('EBCC.exe','file')~=2 )
    disp('ERROR: There are some missing files')
    return
end

%% CREATE THE WEIGHTS AND THE PARAMETERS FILE FOR ALL-PRE
load DataSet.mat

for group=1:4
    PARAMETERS=Parameters{group};
    NUM_GENERATIONS=ngen{group};

    % Generate parameters file
    save file_param.dat PARAMETERS  -ascii
    
    % Generate initial weights
    if group ==1 % only ALL-PRE starts with a NAIVE configuration of weights
        for i=1:NUM_GENERATIONS
            weight(1,1:2)=[24000 0.55];
            weight(2,1:2)=[345444 PARAMETERS(i,7)];
            weight(3,1:2)=[72 0];
            weight(4,1:2)=[10800 PARAMETERS(i,8)];
            weight(5,1:2)=[72 PARAMETERS(i,9)];
            fid=sprintf('WeightsEBCC_%i.dat',i-1);
            filename=fopen(fid,'w');
            for k=1:5
                fprintf(filename,'%d %1.10f\n', weight(k,1), weight(k,2));
            end
            fclose(filename);  
        end
    else % POST groups start with the weights configuration at the end of ALL-PRE (best individual)
        for i=1:NUM_GENERATIONS
            fid=sprintf('WeightsEBCC_%i.dat',i-1);
            copyfile('./SAVED_FILES_Generation_1_Chromosome_37/FinalWeights77.dat',fid)  
        end
    end
    % Simulate models
    disp(['%%%% GENERATION NUMBER ' num2str(group) ' %%%%'])
    parfor individual=0:NUM_GENERATIONS-1
        command = ['EBCC',' ',num2str(group),' ',num2str(individual),' 0'];
        status = system(command);
        while status ~= 0   % Status is 0 when execution is finished and is ok!
                            % While the execution of EDLUT is running, wait
        end   
    end 
end

% Generate simulations (Only the best for each group) saving synaptic weights each trial
% Best of ALL-PRE:
Best{1}=Parameters{1}(38,:);
% Best of SHAM-POST:
Best{2}=Parameters{2}(26,:);
% Best of RIGHT-POST:
Best{3}=Parameters{3}(121,:);
% Best of LEFT-POST:
Best{4}=Parameters{4}(21,:);
for group=1:4
    PARAMETERS=Best{group};

    % Generate parameters file
    save file_param.dat PARAMETERS  -ascii
    
    % Generate initial weights
    if group ==1 % only ALL-PRE starts with a NAIVE configuration of weights
        weight(1,1:2)=[24000 0.55];
        weight(2,1:2)=[345444 PARAMETERS(7)];
        weight(3,1:2)=[72 0];
        weight(4,1:2)=[10800 PARAMETERS(8)];
        weight(5,1:2)=[72 PARAMETERS(9)];
        filename=fopen('WeightsEBCC_0.dat','w');
        for k=1:5
            fprintf(filename,'%d %1.10f\n', weight(k,1), weight(k,2));
        end
        fclose(filename);
        filename=fopen('WeightsEBCC_0_ALLPRE.dat','w');
        for k=1:5
            fprintf(filename,'%d %1.10f\n', weight(k,1), weight(k,2));
        end
        fclose(filename);
    else % POST groups start with the weights configuration at the end of ALL-PRE (best individual)
        copyfile('./SAVED_FILES_Generation_1_Chromosome_37/FinalWeights77.dat','WeightsEBCC_0.dat')     
    end
    % Simulate models
    disp(['%%%% BEST MODELS - GENERATION NUMBER ' num2str(900+group) ' %%%%'])
    command = ['EBCC',' ',num2str(900+group),' 0 1000']; % Saving weights every 1000 ms
    status = system(command);
    while status ~= 0   % Status is 0 when execution is finished and is ok!
                            % While the execution of EDLUT is running, wait
    end       
end

toc
clear
